package com.service.impl;

import com.dao.UserDao;
import com.entity.User;
import com.common.utils.Page;
import com.service.UserService;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * 用户Service接口实现类
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	// 注入UserDao
	@Autowired
	private UserDao userDao;
	// 通过账号和密码查询用户
	@Override
	public User findUser(String usercode, String password) {
		return this.userDao.findUser(usercode, password);
	}
	@Override
	public User findUsername(String username) {
		return this.userDao.findUsername(username);
	}
	@Override
	public User findUserid(String usercode) {
		return this.userDao.findUserid(usercode);
	}
	@Override
	public void addUser(String tel, String password, String username, String maxID) {
		this.userDao.addUser(tel, username, password,maxID);
	}
	@Override
	public String checktel(String tel){
		return this.userDao.checktel(tel);
	}
	@Override
	public String maxID(){
		return this.userDao.maxID();
	}
	public Page<User> findUserList(Integer page, Integer rows,String username) {

			// 创建客户对象
	         User user = new User();
	 		if(StringUtils.isNotBlank(username)){
				user.setUsername(username);
			}
			// 当前页
			user.setStart((page-1) * rows) ;
			// 每页数
			user.setRows(rows);
			// 查询客户列表
			List<User> users = 
	                            userDao.selectUserList(user);
			// 查询客户列表总记录数
			Integer count = userDao.selectUserListCount(user);
			Page<User> result = new Page<>();
			result.setPage(page);
			result.setRows(users);
			result.setSize(rows);
			result.setTotal(count);
			return result;
	}
	@Override
	public User getUserById(Integer id) {
		User user=userDao.findUserById(id);
		return user;
	}
	@Override
	public int updateUser(User user) {
		// TODO Auto-generated method stub
		return userDao.updateUser(user);
	}
	@Override
	public int deleteUser(Integer id) {
		// TODO Auto-generated method stub
		return userDao.deleteUser(id);
	}
	@Override
	public int createUser(User user) {
		// TODO Auto-generated method stub
		return userDao.createUser(user);
	}
}
